//
//  PhoneAreaCodeModel.m
//
//  Created by   on 2018/6/2
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "PhoneAreaCodeModel.h"


NSString *const kPhoneAreaCodeModelCountryCode = @"country_code";
NSString *const kPhoneAreaCodeModelCountryPinYin = @"country_PinYin";
NSString *const kPhoneAreaCodeModelCountryEnglish = @"country_English";
NSString *const kPhoneAreaCodeModelPhoneCode = @"phone_Code";
NSString *const kPhoneAreaCodeModelCountryChinese = @"country_Chinese";


@interface PhoneAreaCodeModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation PhoneAreaCodeModel

@synthesize countryCode = _countryCode;
@synthesize countryPinYin = _countryPinYin;
@synthesize countryEnglish = _countryEnglish;
@synthesize phoneCode = _phoneCode;
@synthesize countryChinese = _countryChinese;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
        self.countryCode = [self objectOrNilForKey:kPhoneAreaCodeModelCountryCode fromDictionary:dict];
        self.countryPinYin = [self objectOrNilForKey:kPhoneAreaCodeModelCountryPinYin fromDictionary:dict];
        self.countryEnglish = [self objectOrNilForKey:kPhoneAreaCodeModelCountryEnglish fromDictionary:dict];
        self.phoneCode = [self objectOrNilForKey:kPhoneAreaCodeModelPhoneCode fromDictionary:dict];
        self.countryChinese = [self objectOrNilForKey:kPhoneAreaCodeModelCountryChinese fromDictionary:dict];
        
    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.countryCode forKey:kPhoneAreaCodeModelCountryCode];
    [mutableDict setValue:self.countryPinYin forKey:kPhoneAreaCodeModelCountryPinYin];
    [mutableDict setValue:self.countryEnglish forKey:kPhoneAreaCodeModelCountryEnglish];
    [mutableDict setValue:self.phoneCode forKey:kPhoneAreaCodeModelPhoneCode];
    [mutableDict setValue:self.countryChinese forKey:kPhoneAreaCodeModelCountryChinese];
    
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    
    self.countryCode = [aDecoder decodeObjectForKey:kPhoneAreaCodeModelCountryCode];
    self.countryPinYin = [aDecoder decodeObjectForKey:kPhoneAreaCodeModelCountryPinYin];
    self.countryEnglish = [aDecoder decodeObjectForKey:kPhoneAreaCodeModelCountryEnglish];
    self.phoneCode = [aDecoder decodeObjectForKey:kPhoneAreaCodeModelPhoneCode];
    self.countryChinese = [aDecoder decodeObjectForKey:kPhoneAreaCodeModelCountryChinese];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeObject:_countryCode forKey:kPhoneAreaCodeModelCountryCode];
    [aCoder encodeObject:_countryPinYin forKey:kPhoneAreaCodeModelCountryPinYin];
    [aCoder encodeObject:_countryEnglish forKey:kPhoneAreaCodeModelCountryEnglish];
    [aCoder encodeObject:_phoneCode forKey:kPhoneAreaCodeModelPhoneCode];
    [aCoder encodeObject:_countryChinese forKey:kPhoneAreaCodeModelCountryChinese];
}

- (id)copyWithZone:(NSZone *)zone
{
    PhoneAreaCodeModel *copy = [[PhoneAreaCodeModel alloc] init];
    
    if (copy) {
        
        copy.countryCode = [self.countryCode copyWithZone:zone];
        copy.countryPinYin = [self.countryPinYin copyWithZone:zone];
        copy.countryEnglish = [self.countryEnglish copyWithZone:zone];
        copy.phoneCode = [self.phoneCode copyWithZone:zone];
        copy.countryChinese = [self.countryChinese copyWithZone:zone];
    }
    
    return copy;
}

#pragma mark - load Data

+ (void)loadPhoneCodeDataOriginalData:(void (^)(NSMutableArray<PhoneAreaCodeModel *> *modelArray))originalData SectionData:(void (^)(NSMutableArray<NSArray<PhoneAreaCodeModel *> *> *sectionArray))sectionData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableArray *dataArray = [NSMutableArray array];
        
        NSData *data = [NSData dataWithContentsOfFile:[kResourceBundle pathForResource:@"PhoneCode" ofType:@"json"]];
        NSError *error = nil;
        
        NSArray<NSDictionary *> *arrayCode = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        if (error)
        {
            NSLog(@"error ----- %@", error);
            return;
        }
        
        [arrayCode enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
         {
             PhoneAreaCodeModel *model = [PhoneAreaCodeModel modelObjectWithDictionary:obj];
             [dataArray addObject:model];
         }];
        
        if (dataArray.count > 0)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                originalData(dataArray);
            });

            
            NSMutableArray *resultArray = [self parserModelArray:dataArray];
            if (resultArray.count > 0)
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    sectionData(resultArray);
                });
            }
        }
        
    });
}

+ (NSMutableArray<NSArray<PhoneAreaCodeModel *> *> *)parserModelArray:(NSMutableArray<PhoneAreaCodeModel *> *)array
{
    NSMutableArray *resultArray = [NSMutableArray array];
    NSArray<NSString *> *charArray = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",
                                   @"h",@"i",@"j",@"k",@"l",@"m",@"n",
                                   @"o",@"p",@"q",@"r",@"s",@"t",@"u",
                                   @"v",@"w",@"x",@"y",@"z"];
    [charArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
    {
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"countryPinYin BEGINSWITH[c] %@",obj];
        NSArray *sectionArray = [array filteredArrayUsingPredicate:predicate];
        if (sectionArray && sectionArray.count > 0)
        {
            [resultArray addObject:sectionArray];
        }
    }];

    return resultArray;
}


@end

